/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.FxConfWithdrawal;
import com.ykht.proj.coupon.service.FxConfWithdrawalService;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;


/**
 * 提现配置表
 *
 * @author proj code generator
 * @date 2021-02-01 19:00:17
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxconfwithdrawal")
@Api(value = "fxconfwithdrawal", tags = "提现配置表管理")
public class FxConfWithdrawalController {

	private final FxConfWithdrawalService fxConfWithdrawalService;

	/**
	 * 查询全部
	 */
	@ApiOperation(value = "查询全部", notes = "查询全部")
	@GetMapping("/findAll")
	@PreAuthorize("@pms.hasPermission('coupon_fxconfposter_view')")
	public R getFxConfWithdrawal() {
		Map<String, Object> map = new HashMap<>();
		map.put("del_flag", 0);
		List<FxConfWithdrawal> list = fxConfWithdrawalService.listByMap(map);
		for (FxConfWithdrawal fxConfWithdrawal : list) {
			String[] split = fxConfWithdrawal.getGears().split(",");
			Integer[] interArray = (Integer[]) ConvertUtils.convert(split, Integer.class);
			Arrays.sort(interArray);
			fxConfWithdrawal.setGears(StringUtils.join(interArray, ","));
		}
		return R.ok(list);
	}


	/**
	 * 通过id查询提现配置表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_fxconfwithdrawal_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(fxConfWithdrawalService.getById(id));
	}

	/**
	 * 新增或修改提现配置表
	 *
	 * @param fxConfWithdrawal 提现配置表
	 * @return R
	 */
	@ApiOperation(value = "新增或修改提现配置表", notes = "新增或修改提现配置表")
	@SysLog("新增或修改提现配置表")
	@PostMapping
	public R save(@RequestBody FxConfWithdrawal fxConfWithdrawal) {
		if (null != fxConfWithdrawal.getGears() && !"".equals(fxConfWithdrawal.getGears())) {
			String[] split = fxConfWithdrawal.getGears().split(",");
			if (fxConfWithdrawal.getFirstMoneyFlag() == 0) {
				if (split.length > 3) return R.failed("首次提现档+其他档位一共最多展示四个");
			} else {
				if (split.length > 4) return R.failed("首次提现档+其他档位一共最多展示四个");
			}
		}
		if (null == fxConfWithdrawal.getId() || "".equals(fxConfWithdrawal.getId())) {
			fxConfWithdrawal.setCreateUser(SecurityUtils.getUserId());
			fxConfWithdrawal.setCreateTime(LocalDateTime.now());
		}
		fxConfWithdrawal.setUpdateUser(SecurityUtils.getUserId());
		fxConfWithdrawal.setUpdateTime(LocalDateTime.now());
		List<FxConfWithdrawal> list = fxConfWithdrawalService.list();
		if (list.size() > 0) {
			FxConfWithdrawal temp = list.get(0);
			fxConfWithdrawal.setId(temp.getId());
		}
		return R.ok(fxConfWithdrawalService.saveOrUpdate(fxConfWithdrawal));
	}

}
